$(function(){
	
	
	
	
	init();
	window.onresize = function(){
		resizeClient();
	};
	
	
	$("#saveButton").click(function(){
		saveToServerD2();
	});
	$("#load_btn").click(function(){
		loadModelD2();
	});
	
});




function startLoad(){
	//两层的回调函数，第一步先检查登录状态，等待信息返回后调用加载右边栏HTML的函数，等待加载完成后调用tabs初始化函数。
	checkSignState(function(online){
		init();
		loadDocTo('http://www.eizia.com/userTabs.html',document.getElementById('userTabs'),
					function(){	document.getElementById("userNameSpace").innerHTML = userName;//读取名字
								startUserTabs(online);
								});
				});
	}

function getAJAXObject(){
	var xmlHttp=null;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
		}
		catch(e){
			// Internet Explorer
			try{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e){
						xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
						}
			}
	return xmlHttp;
	}
	
var AJAX_saveModel;
var AJAX_loadModel;
var capSrc;

function saveToServer(string,name,description){
	
	String.prototype.trim=function() { return this.replace(/(^\s*)|(\s*$)/g,""); }  
	name = name.trim();
	description = description.trim();
	if(description.length == 0){
	alert("名称不能为空！");
		}else if(description.length == 0){
			alert("描述不能为空！");
			}else{
				AJAX_saveModel = getAJAXObject();
				if(AJAX_saveModel==null){
					alert ("Browser does not support HTTP Request");
					return;
					}
					
				capSrc = e1.toDataURL("image/png");
				capSrc = capSrc.slice(22);
				capSrc = capSrc.replace(/\+/g,"%2B");
					
					
				var url="createModelPage.php"
				url = url +"?sid="+Math.random();
				AJAX_saveModel.open("POST",url,true);
				AJAX_saveModel.onreadystatechange = savedTips;	
				AJAX_saveModel.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8");
				AJAX_saveModel.send("state=saveModel&"+
									"stringData="+string+"&"+
									"captureData="+capSrc+"&"+
									"description="+description+"&"+
									"name="+name
									);
				}
	}
	
function savedTips(){
	if (AJAX_saveModel.readyState==4 || AJAX_saveModel.readyState=="complete")
		{ 
		var xmlDoc = AJAX_saveModel.responseXML;
		var newPage = xmlDoc.getElementsByTagName("newPage");
		var info = newPage[0] ? newPage[0].childNodes[0] : false;
		var signNot = xmlDoc.getElementsByTagName("signNot");
		var signAlert = signNot[0] ? signNot[0].childNodes[0] : false;
		
		if(info){
			window.location.href= info.childNodes[0].nodeValue;
			}
		if(signAlert){
			alert ("请在右上角处登录后重试！");
			}	
		
		} 
	}

function wantSave(){
	document.getElementById("cubeTitleButton").style.display = "none";
	document.getElementById("cubeInformation").style.display = "";
	
	var o3dDiv = document.getElementById("o3d");
	o3dDiv.style.width = "640px";
	o3dDiv.style.height = "340px";
	setTimeout(function(){resizeClient();},50);
	
	mouseMode = false;
	document.getElementById("cubeInformationForm").cubeData.value = 
							e110.e111(e62[e62.length-1]);
	e104.cancel();
//////////////////要加上取消所有场景中的对话框///////////////
	}
	
function cancelSave(){
	document.getElementById("cubeTitleButton").style.display="";
	document.getElementById("cubeInformation").style.display = "none";
	
	var o3dDiv = document.getElementById("o3d");
	o3dDiv.style.width = "960px";
	o3dDiv.style.height = "510px";
	setTimeout(function(){resizeClient();},50);
	mouseMode = true;
	}

var AJAX_saveModelD2;
	
	
function saveToServerD2(){
		AJAX_saveModelD2 = getAJAXObject();

				var stringData = worker.save(cubeSelect[cubeSelect.length-1]);
				//TODO:s
				var url="createModelPage.php";
				url = url +"?sid="+Math.random();
				AJAX_saveModelD2.open("POST",url,true);
				AJAX_saveModelD2.onreadystatechange = savedTipsD2;	
				AJAX_saveModelD2.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8");
				AJAX_saveModelD2.send("stringData="+stringData);
				}

	
function savedTipsD2(){
	if (AJAX_saveModelD2.readyState==4 || AJAX_saveModelD2.readyState=="complete")
		{ 
		var id = AJAX_saveModelD2.responseText;
		if(id){
			document.getElementById("id_txt").innerHTML = id;
		}
		else{
			document.getElementById("id_txt").innerHTML = "...";
		}
	} 
}


var AJAX_loadModelD2;

function loadModelD2(){
	var model_id = parseInt(document.getElementById("id_input").value);
	
	AJAX_loadModelD2 = getAJAXObject();
	if(AJAX_loadModelD2==null){
		alert ("Browser does not support HTTP Request");
		return;
		}
	var url="loadModel.php"
	url = url +"?sid="+Math.random();
	AJAX_loadModelD2.onreadystatechange = loadModelTipsD2;
	AJAX_loadModelD2.open("POST",url,true);
	AJAX_loadModelD2.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=ASCII");
	AJAX_loadModelD2.send("model_id="+model_id);
	
	}

function loadModelTipsD2(){
	if (AJAX_loadModelD2.readyState==4 || AJAX_loadModelD2.readyState=="complete"){ 
		var stringData = AJAX_loadModelD2.responseText;
		//TODO:df
		if(stringData){
			worker.create(stringData);
			}
		else{
			//null
			}
		}
	}
